package com.th.supcom.pds.phamapply.server;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.carefx.framework.container.client.exception.UiException;
import net.carefx.framework.util.ConvertUtils;
import net.carefx.framework.web.gwt.server.GwtController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.vo.InpPhamApplyDetailVO;
import com.th.supcom.pds.internal.api.phamapply.InPharmacyReturnService;
import com.th.supcom.pds.phamapply.client.model.InPharmacyReturnResult;
import com.th.supcom.pds.phamapply.client.model.InpPhamApplyDetailModel;
import com.th.supcom.pds.phamapply.client.service.InPharmacyReturnRpc;

/**
 * InpPhamApplyDetail 控制层.
 * 
 * 住院摆药请领明细.
 */
public class InPharmacyReturnServiceController extends GwtController implements InPharmacyReturnRpc
{
    /**
     * 查询
     */
    private InPharmacyReturnService m_inPharmacyReturnService;

    /**
     * 
     */
    private static final long serialVersionUID = -5589159575983246719L;

    /**
     * 默认构造函数.
     */
    public InPharmacyReturnServiceController ()
    {

    }

    @Override
    public InPharmacyReturnResult getInpPhamApplyDetailByApplyNo (String applyNo, String patientId, String performedBy) throws UiException
    {
        s_logger.debug ("进入getInpPhamApplyDetailByApplyNo方法查询退药药品：入参applyNo:{},patientId:{},performedBy:{}", new Object[]
        { applyNo, patientId, performedBy });
        try
        {
            List <List <InpPhamApplyDetailVO>> inpPhamApplyDetailLists = m_inPharmacyReturnService.findInpPhamApplyDetailByApplyNo (applyNo, patientId, performedBy);
            // 结果集
            InPharmacyReturnResult inPharmacyReturnResult = new InPharmacyReturnResult ();
            // 请领明细集合list
            List <List <InpPhamApplyDetailModel>> inpDetailModelLists = new ArrayList <List <InpPhamApplyDetailModel>> ();
            // 请领主记录集合
            List <InpPhamApplyDetailModel> inpDetailModelList = new ArrayList <InpPhamApplyDetailModel> ();
            for (List <InpPhamApplyDetailVO> list : inpPhamApplyDetailLists)
            {
                List <InpPhamApplyDetailModel> inpDetailVOs = ConvertUtils.convert (InpPhamApplyDetailModel.class, list);
                inpDetailModelList.add (inpDetailVOs.get (0));
                inpDetailModelLists.add (inpDetailVOs);
            }
            inPharmacyReturnResult.setInpPhamApplyDetailModels (inpDetailModelList);
            inPharmacyReturnResult.setInpPhamApplyDetailModelsList (inpDetailModelLists);
            return inPharmacyReturnResult;
        }
        catch (Exception e)
        {
            throw new UiException (e.getMessage (), e);
        }
    }

    /**
     * 退药
     */
    @Override
    public void modifyInPhacyReturn (List <InpPhamApplyDetailModel> models, Map <String, Object> infoMap) throws UiException
    {
        s_logger.debug ("进入modifyInPhacyReturn方法申请退药：入参models:【{}】,infoMap:{}", new Object[]
        { models, infoMap });
        List <InpPhamApplyDetailVO> detailVOs = ConvertUtils.convert (InpPhamApplyDetailVO.class, models);
        try
        {
            m_inPharmacyReturnService.modifyInPharmacyReturn (detailVOs, infoMap);
        }
        catch (Exception e)
        {
            throw new UiException (e.getMessage (), e);
        }

    }

    public void setInPharmacyReturnService (InPharmacyReturnService inPharmacyReturnService)
    {
        m_inPharmacyReturnService = inPharmacyReturnService;
    }

    private static final Logger s_logger = LoggerFactory.getLogger (InPharmacyReturnServiceController.class);
    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

}